package com.cungmua.app.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.adapter.MenuAdapter;
import com.cungmua.app.base.BaseActivityScreen;
import com.cungmua.app.model.Category;
import com.cungmua.app.model.City;
import com.cungmua.app.model.EnumMenu;

import java.util.List;

public class MenuListFragment extends BaseActivityScreen {

    private List<Category> categories;
    private  MenuAdapter mAdapter;
    private ImageView btn_back;
    private TextView txt_title;
    private EditText editTextSearch;
    private LinearLayout header_txt_title;

    public MenuListFragment(List<Category> categories) {
        this.categories=categories;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.list, null);
        btn_back=(ImageView)view.findViewById(R.id.btn_back);
        txt_title=(TextView)view.findViewById(R.id.txt_title);
        header_txt_title=(LinearLayout)view.findViewById(R.id.header_txt_title);
        editTextSearch=(EditText)view.findViewById(R.id.editTextSearch);
        btn_back.setOnClickListener(onClickListener);

        editTextSearch.setOnEditorActionListener(new EditText.OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        goToSearch(editTextSearch.getText().toString());
                    return true;
                }
                return false;
            }
        });
		return view;
	}


	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

        mAdapter=new MenuAdapter(getActivity());
        mAdapter.setCategories(categories);
        mAdapter.setSub(false);

		setListAdapter(mAdapter);
        getListView().setOnItemClickListener(onItemClickListener);
        if (isBack()){
            btn_back.setVisibility(View.VISIBLE);
            header_txt_title.setVisibility(View.VISIBLE);
        }else {
            btn_back.setVisibility(View.GONE);
            header_txt_title.setVisibility(View.GONE);
        }


	}

    public View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAdapter.setCategories(categories);
            mAdapter.setSub(false);
            setListAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            setBack(false);
            btn_back.setVisibility(View.GONE);

        }
    };
    public AdapterView.OnItemClickListener onItemClickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Category category=(Category)parent.getAdapter().getItem(position);

            if(category.getId()==33333|category.getId()==11111|category.getId()==1515151515){

            }else{
                if(category.getChildren()==null){
                  if(EnumMenu.HCM.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.HCM.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.HN.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.VT.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.BD.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.CT.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.DN.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.BH.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.NTR.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.OTHER.getName().equals(category.getName())){
                      setCurrentCity(new City(category.getId(),category.getName()));
                      goToHotDeal(true,0);
                  }else if(EnumMenu.LOGIN.getName().equals(category.getName())){
                        login();
                  }else if(EnumMenu.REGISTER.getName().equals(category.getName())){
                        register();
                  }else if(EnumMenu.LOGOUT.getName().equals(category.getName())){
                       logOut();
                  }else if(EnumMenu.PROFILE.getName().equals(category.getName())){
                      goToProfile();
                  }else if(EnumMenu.THONGTIN_DONHANG.getName().equals(category.getName())){
                      goToPastDeals();
                  }
                  else if(EnumMenu.BONUS_POINT.getName().equals(category.getName())){
                      goToBonus();
                  }else if(EnumMenu.CHANGE_PASSWORD.getName().equals(category.getName())){
                      changePassword();
                  }else if(EnumMenu.THONGTIN_HUUICH_ABOUT.getName().equals(category.getName())){
                            //go to about page html
                           goToAbout(HUUICH_ABOUT);
                  }else if(EnumMenu.THONGTIN_HUUICH_QUYCHE.getName().equals(category.getName())){
                        //go to about page html
                      goToAbout(HUUICH_QUYCHE);
                  }else if(EnumMenu.THONGTIN_HUUICH_TRAHANG.getName().equals(category.getName())){
                        //go to about page html
                      goToAbout(HUUICH_HUONGDAN);
                  }else if(EnumMenu.THONGTIN_HUUICH_HUONGDAN.getName().equals(category.getName())){
                    //go to about page html
                      goToAbout(HUUICH_HUONGDAN);
                  }else if(EnumMenu.THONGTIN_HUUICH_HINHTHUC.getName().equals(category.getName())){
                      //go to about page html
                      goToAbout(HUUICH_HINHTHUC);
                  }else if(EnumMenu.THONGTIN_HUUICH_HINHTHUC_GIAO.getName().equals(category.getName())){
                      //go to about page html
                      goToAbout(HUUICH_HINHTHUC_GIAO);
                  }else if(EnumMenu.HOME.getName().equals(category.getName())){
                      goToHotDeal(true,0);
                  }
                  else if(EnumMenu.DANGNHAP.getName().equals(category.getName())){
                      login();
                  }else if(EnumMenu.PREMIUM.getName().equals(category.getName())){
                     goToAccountPremium();
                  }
                  else{
                      goToCategory(category.getId(),category.getName());
                  }

                }else{
                    txt_title.setText(category.getName());
                    setBack(true);
                    mAdapter.setCategories(category.getChildren());
                    mAdapter.setSub(true);
                    setListAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                }
            }
            if (isBack()){
                btn_back.setVisibility(View.VISIBLE);
            }else {
                btn_back.setVisibility(View.GONE);
            }
        }
    };
}
